package cn.hl.hm.controller;

import cn.hl.hm.entity.RechargeRecoardOffline;
import cn.hl.hm.entity.RoadFee;
import cn.hl.hm.logic.service.IRechargeRecoardOfflineService;
import cn.hl.hm.service.RechargeRecoardService;
import cn.hl.hm.service.RoadFeeService;
import cn.hl.hm.util.EmailUtil;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;
import cn.hl.hm.vo.AccountCheckingVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 
 * @author 辅助数据
 *
 */
@Controller
@RequestMapping("/assist")
public class AssistDataController {

	private static final Logger logger = LoggerFactory.getLogger(AssistDataController.class);

	@Autowired
	RoadFeeService roadFeeService;
	@Autowired
	IRechargeRecoardOfflineService recoardOfflineService;
	@Autowired
	RechargeRecoardService rechargeRecoardService;


	@RequestMapping(value = "/roadFee/sendEmail", method = RequestMethod.POST)
	public String saveSecurityFee(@RequestParam String orderCode, HttpServletRequest request,
			HttpServletResponse response) {
		RoadFee roadFee = new RoadFee();
		roadFee.setOrderCode(orderCode);
		List<RoadFee> selectPageList = roadFeeService.selectPageList(roadFee);
		if (selectPageList != null && selectPageList.size() == 1) {
			RoadFee order = selectPageList.get(0);
			Date payTime = order.getPayTime();
			if(payTime!=null) {
				String payTimeStr = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(payTime);
				try {
					EmailUtil.sendEmail(order.getPlateNumber(),
							payTimeStr);
					logger.info("sendEmail>>>>>>>"+order.getOrderCode()+"<<<<<<<<<<<<");
					Result.ok(null, InfoCode.SUCCESS, response);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}else {
				Result.ok(null, InfoCode.PARAM_ERROR, response);
				return null;
			}
		} else {
			Result.ok(null, InfoCode.PARAM_ERROR, response);
			return null;
		}
		return null;
	}

	@RequestMapping(value = "/roadFeeOffline/sendEmail", method = RequestMethod.POST)
	public String saveSecurityFeeOffline(@RequestParam String rechargeCode,
								  HttpServletResponse response) {
		RechargeRecoardOffline rechargeRecoardOffline = new RechargeRecoardOffline();
		rechargeRecoardOffline.setRechargeCode(rechargeCode);
		List<RechargeRecoardOffline> selectPageList = recoardOfflineService.selectByPrimaryKeySelective(rechargeRecoardOffline);
		if (selectPageList != null && selectPageList.size() == 1) {
			rechargeRecoardOffline = selectPageList.get(0);
			String payTimeStr = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(rechargeRecoardOffline.getCreateTime());
			try {
				EmailUtil.sendEmail(rechargeRecoardOffline.getPlateNumber(),
						payTimeStr);
				logger.info("sendEmail>>>>>>>"+rechargeCode+"<<<<<<<<<<<<");
				rechargeRecoardOffline.setSendMailTime(new Date());
				int rows=recoardOfflineService.updateByPrimaryKeySelective(rechargeRecoardOffline);
				if (rows>0) {
					Result.ok(null, InfoCode.SUCCESS, response);
				}else{
					Result.ok(null, InfoCode.FAILURE, response);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			Result.ok(null, InfoCode.PARAM_ERROR, response);
		}
		return null;
	}

	@RequestMapping(value = "/accountChecking/sendEmail", method = RequestMethod.POST)
	public String accountChecking(HttpServletResponse response) {
		try {
			List<AccountCheckingVo> accountCheckingVos = rechargeRecoardService.selectForAccountChecking();
			if (accountCheckingVos != null && accountCheckingVos.size()>0) {
				EmailUtil.sendAccountCheckingEmail(accountCheckingVos);
                Result.ok(null, InfoCode.SUCCESS, response);
            }
		} catch (Exception e) {
			Result.ok(null, InfoCode.FAILURE, response);
			e.printStackTrace();
		}
		return null;
	}
	
	


}
